/**
 *     This file is part of cesm-ws.
 *
 *     cesm-ws, Web Service for Community Earth System Model
 *
 *     Copyright (c) 2010, Purdue University and/or affilicates or thrid-party contributors as
 *     indicated by the @author tags or express copyright attribution statements
 *     applied by the authors. All third-party contributions are distributed under
 *     license by Purdue University.
 *
 *     cesm-ws is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with this distribution.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.purdue.rcac.cesm.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CESMDateUtil {

	public static DateFormat CCSM4_DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd");

	/**
	 * use CCSM4_DATE_FORMAT to parse string and generate a java.util.Date
	 * object
	 * 
	 * @param dateStr
	 * @return
	 * @throws ParseException
	 */
	public static Date String2Date(String dateStr) throws ParseException {
		return String2Date(dateStr, CCSM4_DATE_FORMAT);
	}

	/**
	 * use CCSM4_DATE_FORMAT to format a java.util.Date object
	 * 
	 * @param date
	 * @return
	 */
	public static String Date2String(Date date) {
		return Date2String(date, CCSM4_DATE_FORMAT);
	}

	public static Date String2Date(String dateStr, DateFormat format)
			throws ParseException {
		return format.parse(dateStr);
	}

	public static String Date2String(Date date, DateFormat format) {
		return format.format(date);
	}

	/**
	 * add duration to a Date object, and create a new Date object represents
	 * the end date
	 * 
	 * @param date
	 * @param duration
	 *            A string formated like this: <number>:<indicator>. In CCSM4,
	 *            <number> is known as STOP_N, and <indicator> is known as
	 *            STOP_OPTION.
	 * 
	 *            <pre>
	 *            Valid values for <indicator> are:
	 *                * nday
	 *                * ndays
	 *                * nmonth
	 *                * nmonths
	 *                * nyear
	 *                * nyears
	 * </pre>
	 * 
	 * 
	 * @return end date
	 */
	public static Date addDuration(Date date, String duration) {
		int number = Integer.valueOf(duration.split(":")[0]);
		String indicator = duration.split(":")[1].trim();
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		if ("nday".equals(indicator) || "ndays".equals(indicator)) {
			cal.add(Calendar.DAY_OF_YEAR, number);
		} else if ("nmonth".equals(indicator) || "nmonths".equals(indicator)) {
			cal.add(Calendar.MONTH, number);
		} else if ("nyear".equals(indicator) || "nyears".equals(indicator)) {
			cal.add(Calendar.YEAR, number);
		} else {
			// TODO: exception handle
		}
		return cal.getTime();

	}

	/**
	 * replace <number> part of duration
	 * 
	 * @param duration
	 * @param number
	 * @return
	 */
	public static String replaceNumber(String duration, int number) {
		return String.format("%d:%s", number, duration.split(":")[1]);
	}

	/**
	 * replace <indicator> part of duration
	 * 
	 * @param duration
	 * @param indicator
	 * @return
	 */
	public static String replaceIndicator(String duration, String indicator) {
		return String.format("%s:%s", duration.split(":")[0], indicator);
	}

	/**
	 * a simple test
	 * 
	 * @param args
	 * @throws ParseException
	 */
	public static void main(String[] args) throws ParseException {
		String start = "0002-01-02";
		String duration = "2:nmonths";
		System.out.println("replace number with 14 ["
				+ replaceNumber(duration, 14) + "]");
		System.out.println("replace indicator with nyear ["
				+ replaceIndicator(duration, "nyear") + "]");
		System.out.println("start date: " + start + "\tduration: " + duration);
		System.out.println("\tend date: ["
				+ Date2String(addDuration(String2Date(start), duration)) + "]");
		System.out.println("END");
	}
}
